/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
	\file gpsParser.h
	\brief header file for gpsParser

										\author Jack Elston
						 $Date: 2006/10/25 13:34:45 $
*/

#ifndef __RSSIPARSER_H
#define __RSSIPARSER_H

#include <libtrace.h>
#include <math.h>

#include "threadedPipe.h"

#define RSSI_PERIOD 1

// logging rate - 1 slow rate, - 2 every packet
#define LOGGING_RATE 1 

// exponential weighted moving average, where
// alhpa is smoothing factor [0,1
//    1 -> use instant value
#define ALPHA 0.33 

#define MAX_RSSI_CLIENTS 10

typedef enum counter_dir {
	OUT = 0,
	IN = 1,
} counter_dir_t;

typedef enum counter_type {
	BYTES = 0,
	PACKETS = 1
} counter_type_t;

typedef enum counter_frame {
	MAC = 0,
	IP = 1
} counter_frame_t;

#define MAXCOUNTERTYPE (PACKETS + 1)
#define MAXCOUNTERFRAME (IP + 1)
#define MAXCOUNTERDIR (IN + 1)


class RSSITrace : public ThreadedPipe
{
private:
	uint8_t buf_ptr;
	uint8_t buf[255];

	bool smoothing;
	TimeStamp last_rssi_tx;

	// libtrace
	libtrace_t *trace;
	libtrace_packet_t *packet;
	libtrace_eventobj_t obj;

	struct link_status_str {

		uint8_t  mac[6];
		uint8_t  ip[4];
		float    signal;
		float    noise;
		float    last_rx;

		// rssi filtering using 5th order butterworth with 1Hz cuttoff
		float   xv[BUTTER_NZEROS+1];
		float   yv[BUTTER_NPOLES+1];
		float   noise_xv[BUTTER_NZEROS+1];
		float   noise_yv[BUTTER_NPOLES+1];

		// time stamps
		double lastts;

		// link info
		uint16_t ethertype;
		uint8_t  proto;
		uint16_t icmp;
		uint16_t arp;
		uint16_t udp;
		uint16_t tcp;
		float    rate;
		int8_t   tx_pow;	

		// MAC byte and rate info
		uint64_t num_mac_in;
		uint64_t num_mac_out;
		uint64_t num_mac_packet_in;
		uint64_t num_mac_packet_out;

		float rate_mac_in;
		float rate_mac_out;

		// IP byte and rate info
		uint64_t num_ip_in;
		uint64_t num_ip_out;
		uint64_t num_ip_packet_in;
		uint64_t num_ip_packet_out;

		float rate_ip_in;
		float rate_ip_out;

		// init function
		link_status_str() {
			noise = signal = -1;
			arp=0; icmp = 0; udp = 0; tcp = 0;
			num_mac_in = num_mac_out = 0;
			num_ip_in = num_ip_out = 0;
			last_rx = NAN;
			for(int i=0; i< 6; i++ )
				mac[i] = 0;
			for(int i=0; i< 4; i++ )
				ip[i] = 0;
			rate_ip_in = rate_ip_out = rate_mac_in = rate_mac_out = NAN;
		}

	} linkStat[MAX_RSSI_CLIENTS];

	uint32_t linkCount;
	uint64_t counter[MAXCOUNTERDIR][MAXCOUNTERTYPE][MAXCOUNTERFRAME][MAX_RSSI_CLIENTS];
	void my_per_packet(libtrace_packet_t *packet);

	void update_output_str(int clientInd);
	char outStr[1024];


public:
	RSSITrace(String name);
	bool init(ShmStruct * shmStructPtr);
	~RSSITrace();
					 
	void arp();
	void update();
	void toggleSmoothing(int setVal = -1); // turn on/off smoothing of signal values
};

#endif
